Repository navigation
feat(cli): add --json to void db execute - #920
Conversation
`void db execute --json` writes a JSON array with one D1-shaped
`{ results, success, meta }` entry per statement to stdout, matching
`wrangler d1 execute --json`. Prompts, warnings and errors go to stderr,
and the success line is left out.
- Local SQLite runs the SQL statement by statement. The shared SQL
tokenizer cuts at top-level semicolons, and a piece SQLite reports as
incomplete input (a trigger body) continues into the next one.
- Direct Cloudflare D1 keeps each statement's result instead of merging
their rows.
- PostgreSQL and MySQL read each statement's result from the driver.
- A direct D1 import (a file over 100 KB) and several statements
through the Void platform proxy are refused before anything runs,
since neither can return every statement's rows.
Table output without `--json` is unchanged.
Fixes voidzero-dev#912
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Read SQLite integers as BigInt and MySQL BIGINTs with supportBigNumbers; integers past 2^53 print as strings of their digits. - Print Infinity, -Infinity and NaN as strings instead of null. - Serialize bytes at any depth, so bytea[] elements print as byte arrays. - Split SQLite SQL the way sqlite3_complete() does, so a CREATE TRIGGER is one statement locally and through the platform proxy. - Group a MySQL CALL's result sets and header into one entry. - Leave out PostgreSQL's empty-query result. - Do not open a subcommand menu when --json is passed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Run `cloudflared access login` with `--quiet` and send its output to stderr, so renewing Access never puts the JWT or login text on stdout. - Find MySQL `CALL` statements with MySQL's own lexical rules: `#` comments, `--` only before whitespace, backslash escapes in quotes, and `/*!` / `/*+` comments as SQL. - Give the pg client lossless parsers in JSON mode: `numeric[]` elements stay strings, and numbers in json, jsonb and their arrays keep the digits the database sent. - Skip only the whitespace and comments SQLite skips, so a trailing bare `/*` or nonbreaking space fails as SQLite reports it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SQLite: a byte-order mark where a token starts is whitespace, so a file that begins with one keeps its `CREATE TRIGGER` whole. - MySQL: grouping a `CALL`'s results follows the server's sql_mode and version, read only when the SQL holds a backslash or a `/*!` comment. With NO_BACKSLASH_ESCAPES a backslash is an ordinary character, and a version comment counts only on a server that runs it. - MySQL: a control character after `--` starts a comment, and `/*+` hints outside a statement are ordinary comments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…te --json` CALL results - Read the server mode on a separate connection so the user's session state (FOUND_ROWS(), ROW_COUNT(), warnings) is untouched. - Treat BEGIN as a block opener only in stored programs and MariaDB's BEGIN NOT ATOMIC, not as a column name or XA BEGIN. - Follow MariaDB's executable comments: /*M!, six-digit versions, and skipping /*! comments for versions 50700 to 99999. - Read double quotes as identifiers in ANSI_QUOTES mode. - Allow one nested comment inside a skipped version comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…son` output exact - Read later statements with the mode a `SET sql_mode` to a quoted list leaves, and refuse before running SQL whose CALL results could not be grouped after an unreadable change - Read reserved words after a dot (`t.case`, `t.end`) as names - Keep `ALTER EVENT ... DO BEGIN ... END` bodies in one statement - Print MySQL dates and times as the text MySQL returns - Leave --json out of the interactive `void db` menu, which draws on stdout - Drop URL options that would change JSON output, such as `debug`, from both connections Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…id db execute --json` MySQL and MariaDB: - Leave out comments after the last statement, which the server answers with a result of their own, so a CALL's results stay grouped. - Group the results of an EXECUTE of a prepared CALL, MariaDB's EXECUTE IMMEDIATE and SET STATEMENT ... FOR, and compound statements MariaDB runs outside a stored program. Refuse an EXECUTE of a statement the SQL does not prepare from a quoted string. - Keep stored program bodies without BEGIN together (IF, CASE, LOOP, WHILE, REPEAT, FOR), in procedures, functions, triggers and events. - After an unclear SET sql_mode, read the SQL in each quoting mode the server may be in, rather than refusing whenever a CALL follows. - Read `@case` as a variable, `begin`, `end` and `do` as names where a stored program uses them so, a handler's statement as a statement, and the account in DEFINER = account as no object. - Read `[name]` as a quoted name in MariaDB's MSSQL mode. Local D1: report the rows a statement's triggers write in `meta.changes`, as D1 does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d more MySQL SQL exactly - Send anything else written to stdout, such as output from void.config.ts on each load, to stderr from the start of the command until the process exits. - Read the dot after digits as a decimal point, so `1.` before END closes a CASE expression. - Read the server's mode for SQL with double quotes, so ANSI_QUOTES names of prepared statements resolve, and set aside readings where a string names a prepared statement, which the server rejects. - Report MySQL insert IDs of 2^63 and more as unsigned; mysql2 reads them as signed. - Keep a SQLite column named __proto__ in the rows. - Let Cloudflare Access sign in with a terminal on stdin and stderr, so piping stdout no longer prevents renewing a rejected credential. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks! There are two issues:
|
… process output off `void db execute --json` stdout
The Cloudflare Access sign-in writes to stdout again when it is a terminal, so an
interactive command run with `2>log` can renew an expired session. It uses stderr
only while stdout is redirected or kept for `--json` results.
While `--json` keeps stdout for its results, child processes that would inherit
stdout (for example one void.config.ts starts with `execFileSync(..., { stdio:
'inherit' })`) get stderr instead.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks! Both are fixed in 176d2db.
|
|
One more issue: |
…rint PostgreSQL dates and MySQL JSON exactly Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks! Fixed in fff7e0d. Child stderr on stdout: While I also fixed two related ways the output could differ from what the database sends:
|
Closes #912.
void db executeonly printed a text table. The table can't tellNULLfrom'NULL'or1from'1', a value with a newline splits its row, and SQL with several statements printed nothing.--jsonprints a JSON array to stdout with one entry per statement, in the shape of a D1 result (the same aswrangler d1 execute --json):stdout holds only that array. Prompts, warnings, errors and any other output (for example a
console.loginvoid.config.ts) go to stderr. A failure exits non-zero with stdout empty. Without--json, nothing changes.Per database
metasqlite3_complete()), so aCREATE TRIGGER … END;stays one statementchanges(trigger writes included, as D1 counts them),last_row_id,durationcommand,rowCountaffectedRows,insertIdA direct D1 import (a
--fileover 100 KB) returns no rows, so--jsonis refused before the upload.MySQL: one entry per CALL
A MySQL
CALLreturns each of its procedure's result sets and then a header. These are grouped into one entry, so the array still has one entry per statement. The same goes for anEXECUTEof a preparedCALLand for compound statements MariaDB runs outside a procedure (BEGIN NOT ATOMIC,IF,LOOP, ...).To find the statements, the SQL is read the way the server reads it before it runs:
sql_mode;/*!, MariaDB's/*M!) for the server's version;SET sql_modechanges in the SQL itself.When the SQL depends on the server's mode,
--jsonfirst readssql_modeandVERSION()on a separate connection. It only does this when the SQL contains a backslash, a double quote, a[or an executable comment.--jsonrefuses, before running anything, when it can't tell which results belong to which statement:EXECUTEof a statement not prepared from a quoted string, such asPREPARE s FROM @sql;SET sql_mode = @savedafter which the rest of the SQL could split differently, with aCALLamong the statements.If the results still don't line up with the statements, it prints one entry per result rather than guess.
Values
last_row_idand MySQLinsertId. mysql2 readsinsertIdas signed, so IDs of 2^63 or more are converted back to unsigned.Infinity,-InfinityandNaNprint as strings, notnull.numeric[]elements print as strings, and numbers insidejson/jsonbkeep their digits.Design choices
--remote --jsonin a terminal still asks first. The prompt is on stderr. wrangler's--jsonskips it.--json | jqcan still renew a rejected credential.void db --jsonin a terminal no longer opens the subcommand menu, which drew on stdout. It prints a usage error instead.Known limits
--json. It still runs without--json.__proto__. SQLite and PostgreSQL keep it.CALL's entry combines the rows of all its result sets.Tests
db-commands.test.ts: local SQLite end to end.db-execute-remote.test.ts: D1, PostgreSQL and MySQL paths.db-execute-results.test.ts: statement splitting and grouping.cf-access.test.ts: Access sign-in.vp check,vp run testandvpr test:allpass.Docs
docs/reference/cli/database.md: new "JSON output" section.🤖 Generated with Claude Code